package com.westcom.secritycheck.SecurityGate;

import com.westcom.secritycheck.WaitingLine.IWaitingLine;
import com.westcom.secritycheck.bean.OperationException;
import com.westcom.secritycheck.bean.Passenger;

import javax.swing.*;
/*
* auth:zoupeng
* date:20180904
* */
public abstract class SecurityGate
{
    private int checkPeriod;
    private Timer checkTimer;
    protected IWaitingLine line = null;
    abstract public boolean check(Passenger p);
    abstract public void onPass(Passenger p );
    abstract public void onDenied(Passenger p);

    public SecurityGate(int checkPeriod,IWaitingLine line)throws OperationException
    {
        if(checkPeriod <= 0||null == line)
        {
            throw new OperationException("invalid param");
        }
        this.checkPeriod = checkPeriod;
        this.line=  line;
        this.checkTimer = new javax.swing.Timer(this.checkPeriod
        ,e->
        {
            Passenger p = this.line.offer();
            if(null == p)
            {
                return;
            }
            boolean passed = check(p);
            if(passed)
            {
                onPass(p);
            }else
            {
                onDenied(p);
            }
            return;
        });
    }
    public void startCheck()
    {
        checkTimer.start();
    }
    public void endCheck()
    {
        checkTimer.stop();
    }
}
